package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.dto.PrizeQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.Prize;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.PrizeService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/prize")
public class PrizeController {
    @Autowired
    ColumnInfoService columnInfoService;
    @Autowired
    PrizeService prizeService;

    /**
     * 获取 友联列表
     *
     * @return
     */
    @RequestMapping("/list")
    public String index(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String title = request.getParameter("title");

        PrizeQueryDTO prizeQueryDTO = new PrizeQueryDTO();
        prizeQueryDTO.setTitle(title);


        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PrizeQueryDTO prizeeQueryDTO = new PrizeQueryDTO();
        prizeeQueryDTO.setTitle(title);
        prizeeQueryDTO.setCurrentPage(currentPage);
        prizeeQueryDTO.setPageSize(pageSize);

        PageModel<Prize> page = prizeService.queryPrizePage(prizeeQueryDTO);

        List<Prize> list = page.getList();
        list.forEach(prize -> {
            String columnId = prize.getColumnId();
            if (StringUtils.isNotBlank(columnId)) {
                ColumnInfo columnInfo = columnInfoService.find(columnId);
                prize.setPrizeName(columnInfo.getName());
            }

        });

        model.addAttribute("page", page);
        model.addAttribute("certificateQueryDTO", prizeeQueryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_PRIZE);
        return "/cms/prize/prize_list";
    }

    /**
     * 跳转到友情编辑页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model) {

        String id = request.getParameter("id");
        Prize prize = prizeService.find(id);

        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setCode(Constants.CODE_ZYJX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> columnInfos = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List<ColumnInfo> prizeCateList = null;

        if (!CollectionUtils.isEmpty(columnInfos)) {
            ColumnInfoQueryDTO parentColumnInfoQueryDTO = new ColumnInfoQueryDTO();
            parentColumnInfoQueryDTO.setIsRootColumnLike(false);
            parentColumnInfoQueryDTO.setRootColumnId(columnInfos.get(0).getId());
            prizeCateList = columnInfoService.queryColumnInfoList(parentColumnInfoQueryDTO);
        }

        model.addAttribute("prize", prize);
        model.addAttribute("prizeCateList", prizeCateList);

        return "/cms/prize/dialog/prize_edit";
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doFriendShipAjaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String title = request.getParameter("title");
            String description = request.getParameter("description");
            String imgUrl = request.getParameter("imgUrl");
            String columnId = request.getParameter("columnId");
            Prize prize = new Prize();
            prize.setId(id);
            prize.setTitle(title);
            prize.setDescription(description);
            prize.setImgUrl(imgUrl);
            prize.setColumnId(columnId);
            if (StringUtils.isNotBlank(id)) {
                prizeService.updateSelective(prize);
            } else {
                prizeService.save(prize);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/del")
    @ResponseBody
    public AjaxResult doFriendShipAjaxDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            prizeService.delete(id);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
